using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace enviametuimagen
{
    public partial class FormEnvio : Form
    {
        List<string> files;
        public FormEnvio()
        {
            InitializeComponent();
            imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
            files = new List<string>();
        }

        private void enviar_Click(object sender, EventArgs e)
        {
            if (nombre.Text == null || nombre.Text.Trim().Equals(""))
            {
                MessageBox.Show("Escribi el nombre del nene o nena.");
                return;
            }
            if (files.Count < 1)
            {
                MessageBox.Show("No agregastes imagenes para enviar.");
                return;
            }

            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("mailemisor@gmail.com");
                mail.To.Add("mailreceptor@gmail.com");
                mail.Subject = "fotos "+nombre.Text;
                mail.Body = "aca van las imagenes de "+nombre.Text;

                foreach (string archivo in files)
                {

                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment(archivo);
                    mail.Attachments.Add(attachment);
                }
                

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("mailemisor", "password");
                SmtpServer.EnableSsl = true;
                SmtpServer.SendCompleted += new SendCompletedEventHandler(SmtpServer_SendCompleted);
                progreso.Style = ProgressBarStyle.Marquee;
                SmtpServer.SendAsync(mail,"ok");
            }
            catch (Exception)
            {
                MessageBox.Show("Ocurrio un error");
            }
        }

        void SmtpServer_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if(e.UserState.Equals("ok"))
            {
                progreso.Value = 100;
                progreso.Style = ProgressBarStyle.Blocks;
                MessageBox.Show("Las imagenes se enviaron exitosamente. Gracias.");
                nombre.Text = "";
                listView.Items.Clear();
                imageList.Images.Clear();
                files.Clear();
                progreso.Value = 5;
            }

        }

        void dropper_DragEnter(object sender, DragEventArgs e)
        {
            // Check if the Dataformat of the data can be accepted
            // (we only accept file drops from Explorer, etc.)
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy; // Okay
            else
                e.Effect = DragDropEffects.None; // Unknown data, ignore it

        }

        // Occurs when the user releases the mouse over the drop target 
        void dropper_DragDrop(object sender, DragEventArgs e)
        {
            // Extract the data from the DataObject-Container into a string list
            string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            // Do something with the data...
            
            // For example add all files into a simple label control:
            foreach (string archivo in FileList)
            {
                files.Add(archivo);
                imageList.Images.Add(Image.FromFile(archivo));
            }

            listView.Items.Clear();
            // Release the graphics device context.
            for (int i = 0; i < imageList.Images.Count; i++)
            {
                listView.Items.Add(new ListViewItem(""+i,i));
            }
            
        }

        private void salir_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void limpiar_Click(object sender, EventArgs e)
        {
            listView.Items.Clear();
            imageList.Images.Clear();
            files.Clear();
        }
    }
}
